<?php
/**
 * Created by PhpStorm.
 * User: 郑好
 * Date: 2019/3/19
 * Time: 下午2:19
 */

namespace app\common;

use PHPMailer\PHPMailer\PHPMailer;
use Log;

class SMTP
{
    use InstanceTrait;

    public function send($email, $subject, $content, $isHtml = false)
    {
        $mail = new PHPMailer;

        $mail->isSMTP();

        $mail->CharSet = "UTF-8";

        $mail->Encoding = 'base64';

        $mail->SMTPDebug = config("app_env") == "local" ? 1 : 0; //是否调试

        $mail->SMTPAuth = true;

        $mail->Host = "smtp.mxhichina.com";

        $mail->Port = config("app_env") == "local" ? 25 : 465;

        $mail->Username = "zhenghao@xmcmit.com";

        $mail->Password = "CMIT#aliyx@1005";

        $mail->setFrom("zhenghao@xmcmit.com", "福祥天下 社区"); // 发件人邮箱和名称

        $mail->Subject = $subject;

        $mail->Body = $content;

        $mail->isHTML($isHtml); //是否启用html

        $mail->addAddress($email); //收件用户

        $mail->send();

        if (!$mail->send()) {
            \Log::warning("邮件发送失败： ".$mail->ErrorInfo, []);
            return ["code" => _MSG_SYSTEM_ERROR, "msg" => "邮件发送失败"];
        }
        return ["code" => _MSG_SYSTEM_SUCCESS, "msg" => "邮件发送成功"];
    }
}
